﻿using CustomORM.DataAccess;
using CustomORM.Mapping;
using CustomORM.Repository;
using NUnit.Framework;
using Rhino.Mocks;

namespace CustomORM.Tests.CustomORM
{
    [TestFixture]
    public class CriteriaTests
    {
        [SetUp]
        public static void SetUp()
        {
            MagicMapper.RemoveMapper(typeof(ParentEntity));
            MagicMapper.RemoveMapper(typeof(KidEntity));
            new ParentEntityMap();
            new KidEntityMap();
        }

        [Test]
        public void CreateCriteriaForParentAndjoinChildren_CriteriaAllowsFluentSysntaxes()
        {
            Criteria criteria = new Criteria(typeof(ParentEntity), "parent")
                .AddEntity(typeof(KidEntity), "kid");

            Assert.That(criteria, Is.Not.Null);
            Assert.That(criteria.Participants, Is.Not.Null);
            Assert.That(criteria.Participants.Count, Is.GreaterThan(0));
            Assert.That(criteria.Participants[0].Mapper, Is.TypeOf(typeof(ParentEntityMap)));
            Assert.That(criteria.Participants[0].Alias, Is.EqualTo("parent"));
            Assert.That(criteria.Participants[1].Mapper, Is.TypeOf(typeof(KidEntityMap)));
            Assert.That(criteria.Participants[1].Alias, Is.EqualTo("kid"));
        }

        [Test, Ignore("This test should be rewritten")]
        public void GenerateSqlStatement_GeneratesExpectedStatement()
        {
            Criteria criteria = new Criteria(typeof(ParentEntity), "p")
                .AddEntity(typeof(KidEntity), "k");
                
            string selectSql = criteria.GenerateSelectForPart();

            Assert.That(selectSql.ToUpper(), Is.EqualTo("SELECT p.PARENT_ID as _1PARENT_ID, p.NAME as _1NAME, k.KID_ID as _2KID_ID, k.PARENT_ID as _2PARENT_ID, k.NAME as _2NAME FROM PARENT p ".ToUpper()));
        }

        [Test, Ignore("This test should be rewritten")]
        public void AddFreeSQLStatement_GeneratesExpectedStatement()
        {
            var parentId = 7;
            Criteria criteria = new Criteria(typeof(ParentEntity), "p")
                .AddEntity(typeof(KidEntity), "k")
                .AddSQL("join KID k on k.PARENT_ID = p.PARENT_ID where PARENT_ID = {0}", parentId);

            string selectSql = criteria.GenerateCompleteSelect();

            var expected = "SELECT p.PARENT_ID as _1PARENT_ID, p.NAME as _1NAME, k.KID_ID as _2KID_ID, k.PARENT_ID as _2PARENT_ID, k.NAME as _2NAME FROM PARENT p join KID k on k.PARENT_ID = p.PARENT_ID where PARENT_ID = 7 ";
            Assert.That(selectSql.ToUpper(), Is.EqualTo(expected.ToUpper()));
        }
    }
}